export default {
  namespaced: true,
  state: () => ({
    // 收货地址
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
    // token: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
    token: JSON.parse(uni.getStorageSync('token')) || '',
    // 重定向的 object 对象
    redirectInfo: null
  }),
  
  mutations: {
    saveAddress(state){
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 更新收货地址
    updateAddress(state, address) {
      state.address = address
      this.commit('m_user/saveAddress')
    },
    // 更新用户的基本信息
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
      this.commit('m_user/saveUserInfo')
    },
    saveUserInfo(state) {
      uni.setStorageSync('userInfo', JSON.stringify(state.address))
    },
    setToken(state) {
      state.token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
    removeToken(state) {
      state.token = ''
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
    // 更新重定向的信息对象
    updateRedirectInfo(state, info) {
      state.redirectInfo = info
    }
  },
  
  getters: {
    // 收货详细地址的计算属性
    addStr(state) {
      if (!state.address.provinceName) return ''
      
      return state.address.provinceName +
      state.address.cityName +
      state.address.countyName +
      state.address.detailInfo 
    }
  }
}